package com.lixinyue.p2.ui;

import com.lixinyue.p2.bean.Customer;
import com.lixinyue.p2.service.CustomerList;
import com.lixinyue.p2.util.CMUtility;

public class CustomerView {
    CustomerList customerList=new CustomerList(10);


    public CustomerView()
    {
        Customer customer = new Customer("张三", '男', 19, "13109091122", "zs@qq.com");
        customerList.addCustomer(customer);
    }



    /**
     *
     */
    public  void  enterMainMenu()
    {
        boolean flag=true;
        do{
            System.out.println("-----------------------客户信息管理界面-----------------------\n            ");
            System.out.println("                        1.添加客户  ");
            System.out.println("                        2.修改客户  ");
            System.out.println("                        3.删除客户  ");
            System.out.println("                        4.查看客户  ");
            System.out.println("                        5.退        出  ");
            System.out.print("                        请选择1-5:");
            char key=CMUtility.readMenuSelection();
            switch(key)
            {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;

                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    System.out.println("请确认是否退出（Y/N）");
                    char isExit=CMUtility.readConfirmSelection();
                    if(isExit=='Y')
                    {
                        flag=false;
                    }
            }
        }while(flag);
    }


    private void addNewCustomer()
    {
        System.out.println("-----------------------添加客户-----------------------");
        System.out.print("姓名："+"\n");
        String name=CMUtility.readString(10);
        System.out.print("性别："+"\n");
        char gender=CMUtility.readChar();

        System.out.print("年龄："+"\n");
        int age=CMUtility.readInt();
        System.out.print("手机号："+"\n");
        String phone=CMUtility.readString(13);
        System.out.print("邮箱："+"\n");
        String email=CMUtility.readString(30);

        Customer customer1 = new Customer(name,gender,age,phone,email);
        boolean issucceed=customerList.addCustomer(customer1);
        if(issucceed)
        {
            System.out.println("-----------------------添加成功-----------------------");
        }
        else
        {
            System.out.println("-----------------------添加失败-----------------------");
        }


    }
    private  void  modifyCustomer()
    {
        System.out.println("-----------------------修改客户-----------------------");
        Customer cust;
        int num;
        for (;  ; ) {
            System.out.print("请选择要修改的客户编号(-1为退出选择):");
            num=CMUtility.readInt();
            if(num==-1)
            {
                return;
            }
            cust=customerList.getCutomer(num-1);
            if(cust==null)
            {
                System.out.println("无法找到此用户！");
            }
            else
            {
                break;
            }

        }
        //修改客户信息
        System.out.print("姓名("+cust.getName()+"):");
        String name=CMUtility.readString(10,cust.getName());
        System.out.print("性别("+cust.getGender()+"):");
        char gender=CMUtility.readChar(cust.getGender());
        System.out.print("年龄("+cust.getAge()+"):");
        int age=CMUtility.readInt(cust.getAge());
        System.out.print("手机号("+cust.getPhone()+"):");
        String phone=CMUtility.readString(13,cust.getPhone());
        System.out.print("邮箱("+cust.getEmail()+"):");
        String email=CMUtility.readString(14,cust.getEmail());
         cust=new Customer(name,gender,age,phone,email);
<<<<<<< HEAD
         boolean isReplaced=customerList.repalceCustomer(num,cust);
=======
         boolean isReplaced=customerList.repalceCustomer(num-1,cust);
>>>>>>> fa12ee9ecafd9edbc7a052a7c72d1884d48813ea
         if(isReplaced)
         {
             System.out.println("-----------------------修改客户成功-----------------------");
         }
         else
         {
             System.out.println("-----------------------修改客户失败-----------------------");
         }

    }
    private  void  deleteCustomer()
    {
        int num;
        System.out.println("-----------------------删除客户-----------------------");
        for (;; ) {
            System.out.print("请选择你要删除的客户(-1请退出):");
            num=CMUtility.readInt();
            if(num==-1)
            {
                return;
            }else{
                Customer cust=customerList.getCutomer(num-1);
                if(cust==null)
                {
                    System.out.println("客户不存在,请重新输入。");
                }
                else {
                    break;
                }
            }

        }
        //执行删除
        System.out.print("是否确定删除(Y/N):");
        char key = CMUtility.readConfirmSelection();
        if(key=='Y')
        {
            boolean isDeleted=customerList.deleteCustomer(num-1);
            if(isDeleted)
            System.out.println("-----------------------删除客户成功-----------------------");
            else {
                System.out.println("-----------------------删除客户失败-----------------------");
            }
        }
    }
    private  void  listAllCustomers()
    {
        System.out.println("-----------------------查询客户-----------------------");

        if(customerList.getTotal()==0)
        {
            System.out.println("没有客户信息。");
        }
        else
        {
            System.out.println("编号 姓名   性别 年龄 \t手机号\t\t邮箱");
            Customer[] custs=customerList.getAllCustomer();
            for (int i = 0; i <custs.length ; i++) {
                Customer cust=custs[i];
                System.out.println((i+1)+"\t"+cust.getName()+"\t"  + cust.getGender()
                        + "\t"+ + cust.getAge()+"\t"+  cust.getPhone() +"\t" + cust.getEmail());

            }

        }
        System.out.println("-----------------------客户信息列表完成。-----------------------");

    }

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();

    }

}
